//
//  FP_UserPriceInquiryVC.m
//  FootPad
//
//  Created by 张浩 on 2022/2/16.
//  Copyright © 2022 胶享悦. All rights reserved.
//

#import "MM_HomeStockWarnVC.h"
#import "MM_HomeStockWarnCell.h"
#import "MM_CodeResultListVC.h"
#import "MM_HomeSearchHeaderView.h"
#import "MM_HomeSearchDataView.h"

static const CGFloat AddTitleTag = 100;
static const CGFloat cateH = 32;

@interface MM_HomeStockWarnVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page_num;
@property (nonatomic, strong) MM_HomeSearchHeaderView *searchHeaderView;
@property (nonatomic, strong) MM_HomeSearchDataView *searchDataView;
@property (nonatomic, copy) NSString *goodSearchStr;
@property (nonatomic, copy) NSString *locationNumberSearchStr;
@property (nonatomic, copy) NSString *old_code_str;

@end

@implementation MM_HomeStockWarnVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.titleView.hidden = YES;
    
    [self.view addSubview:self.searchHeaderView];
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.searchDataView];

    [self setTitleUI];
    
    [self setupRefresh];
    self.page_num = 1;
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [self loadData];
    [self libraryGoodsData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MM_HomeStockWarnCell *cell = [MM_HomeStockWarnCell cellWithTableView:tableView];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MM_HomeStockListModel *model = self.dataArr[indexPath.row];
    
    MM_CodeResultListVC *listVC = [[MM_CodeResultListVC alloc] init];
    listVC.code_str = model.sku_code;
    [self.navigationController pushViewController:listVC animated:YES];
}

- (void)setTitleUI {
    
    NSArray *titleArr = @[@"商品名称",@"商品规格",@"库存",@"预警数量"];
    NSArray *titleWArr = @[@(9),@(6),@(5),@(5)];
    CGFloat titleW = ScreenW /24.0;
    CGFloat titleX = 0;
    
    for (UIView*subView in self.scrollView.subviews) {
        [subView removeFromSuperview];
    }
    
    for (int i=0; i<titleArr.count; i++) {
        
        UIView *titleView = [[UIView alloc] init];
        titleView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#EEEEEE"];
        titleView.tag = AddTitleTag + i;
        [self.scrollView addSubview:titleView];
        
        [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.equalTo(titleX);
            make.top.equalTo(self.scrollView);
            make.width.equalTo(titleW*[titleWArr[i] integerValue]);
            make.height.equalTo(40);
        }];
        
        titleX += titleW*[titleWArr[i] integerValue];
        
        UILabel *nameLabel = [Custom_ZHTool uiLabelCreate:titleArr[i] Font:FONT(13) TextColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
        nameLabel.text = titleArr[i];
        nameLabel.font = FONT_BOLD(12);
        [titleView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (i == 0) {
                make.left.equalTo(12);
            }else {
                make.left.equalTo(10);
            }
            
            make.centerY.equalTo(titleView);
        }];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#DDDDDD"];
        [titleView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {

            make.right.top.equalTo(titleView);
//            make.top.equalTo(self.scrollView);
            make.width.equalTo(0.5);
            make.height.equalTo(40);
        }];

    }
    
    self.tableView.MM_w = titleX;
    self.scrollView.contentSize = CGSizeMake(titleX, self.scrollView.MM_h);
    
    [self.scrollView addSubview:self.tableView];
}

- (void)loadData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"pageSize"] = @"20";
    parmDic[@"pageIndex"] = @(self.page_num).description;
    
    NSMutableArray *tempArr = [NSMutableArray array];
    
    if (!stringIsEmpty(self.goodSearchStr)) {
        
//        if ([Custom_ZHTool checkIsAllNumberWithString:self.goodSearchStr]) {
//            
//            NSMutableDictionary *goodParmDic = [NSMutableDictionary dictionary];
//            goodParmDic[@"name"] = @"sku_id";
//            goodParmDic[@"operator"] = @"1";
//            goodParmDic[@"text"] = self.goodSearchStr;
//            goodParmDic[@"value"] = self.goodSearchStr;
//            [tempArr addObject:goodParmDic];
//
//            NSMutableDictionary *skuParmDic = [NSMutableDictionary dictionary];
//            skuParmDic[@"name"] = @"sku_name";
//            skuParmDic[@"operator"] = @"6";
//            skuParmDic[@"condition"] = @"1";
//            skuParmDic[@"text"] = self.goodSearchStr;
//            skuParmDic[@"value"] = self.goodSearchStr;
//            [tempArr addObject:skuParmDic];
//            
//        } else {
            
            NSMutableDictionary *spuParmDic = [NSMutableDictionary dictionary];
            spuParmDic[@"name"] = @"spu_name";
            spuParmDic[@"operator"] = @"6";
            spuParmDic[@"text"] = self.goodSearchStr;
            spuParmDic[@"value"] = self.goodSearchStr;
            [tempArr addObject:spuParmDic];
            
//            NSMutableDictionary *skuParmDic = [NSMutableDictionary dictionary];
//            skuParmDic[@"name"] = @"sku_name";
//            skuParmDic[@"operator"] = @"6";
//            skuParmDic[@"condition"] = @"1";
//            skuParmDic[@"text"] = self.goodSearchStr;
//            skuParmDic[@"value"] = self.goodSearchStr;
//            [tempArr addObject:skuParmDic];
//
//        }

    }
    
    if (!stringIsEmpty(self.locationNumberSearchStr)) {
        
        NSMutableDictionary *numberParmDic = [NSMutableDictionary dictionary];
        numberParmDic[@"name"] = @"location_name";
        numberParmDic[@"operator"] = @"6";
        numberParmDic[@"text"] = self.locationNumberSearchStr;
        numberParmDic[@"value"] = self.locationNumberSearchStr;
        [tempArr addObject:numberParmDic];
    }
    
    if (!stringIsEmpty(self.old_code_str)) {
        
        NSMutableDictionary *old_codeParmDic = [NSMutableDictionary dictionary];
        old_codeParmDic[@"name"] = @"old_code";
        old_codeParmDic[@"operator"] = @"1";
        old_codeParmDic[@"text"] = self.old_code_str;
        old_codeParmDic[@"value"] = self.old_code_str;
        [tempArr addObject:old_codeParmDic];
    }
    
    parmDic[@"searchObjects"] = tempArr;

    [MM_Http postRequestURLStr:KMM_Stock_StockAlert withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        [self endRefersh];
        
        NSString *total_count = [requestDic[@"totals"] description];

        NSArray *dataArr = [MM_HomeStockListModel mj_objectArrayWithKeyValuesArray:requestDic[@"rows"]];
        
        if (self.page_num <= 1) {
            [self.dataArr removeAllObjects];
        }
        
        [self.dataArr addObjectsFrom_MyArr:dataArr];
        [self.tableView reloadData];

        if (self.dataArr.count <= 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            if (total_count.integerValue == self.dataArr.count && self.dataArr.count > 0) {
                
                if (self.dataArr.count > 10) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }
                
            }else {
                [self.tableView.mj_footer endRefreshing];
            }
        }
        NSLog(@"requestDicrequestDicrequestDic == %@",requestDic);
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self endRefersh];
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (void)libraryGoodsData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    
    [MM_Http postRequestURLStr:KMM_Warehousearea_List withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        
        NSArray *dataArr = [MM_HomeSearchLeftModel mj_objectArrayWithKeyValuesArray:requestDic[@"rows"]];
        
        self.searchDataView.leftArr = dataArr;
        MMLog(@"requestDicrequestDicrequestDic == %@",requestDic);
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self showStateText:errorInfo];
    }];
    
    [MM_Http postRequestURLStr:KMM_Goodslocation_List withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        
        NSArray *dataArr = [MM_HomeSearchRightModel mj_objectArrayWithKeyValuesArray:requestDic[@"rows"]];
        self.searchDataView.rightArr = dataArr;
        MMLog(@"requestDicrequestDicrequestDic == %@",requestDic);
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self showStateText:errorInfo];
    }];
}

- (UIScrollView *)scrollView {
    
    if (_scrollView == nil) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.searchHeaderView.MM_Max_Y, ScreenW, ScreenH - NaviBarHeight - self.searchHeaderView.MM_h - HomeIndicatorHeight - 44)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

- (MM_HomeSearchHeaderView *)searchHeaderView {
    
    if (_searchHeaderView == nil) {
               
        _searchHeaderView = [[MM_HomeSearchHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 105)];
        
        WS(weakSelf);
        _searchHeaderView.searchGoodsBlock = ^(NSString * _Nonnull goodsSearchStr) {
          
            //商品名称
            weakSelf.goodSearchStr = goodsSearchStr;
        };
        
        _searchHeaderView.searchShippingSpaceBlock = ^(NSString * _Nonnull shippingSpaceSearchStr) {
            
            //仓位号
            weakSelf.locationNumberSearchStr = shippingSpaceSearchStr;
        };
        
        _searchHeaderView.oldCodeBlock = ^(NSString * _Nonnull old_code) {
            
            weakSelf.old_code_str = old_code;
        };
        
        _searchHeaderView.searchFirstResponseBlock = ^(NSInteger index) {
          
            //第一响应
            if (index == 0) {
                weakSelf.searchDataView.hidden = YES;
            } else {
                weakSelf.searchDataView.hidden = NO;
            }
        };
        
        _searchHeaderView.searchInquireBlock = ^(NSInteger index) {
          
            //查询 请求数据
            [weakSelf.view endEditing:YES];
            [weakSelf showHudInView:weakSelf.view hint:@"正在加载数据..."];
//            if (!stringIsEmpty(weakSelf.goodSearchStr) || !stringIsEmpty(weakSelf.locationNumberSearchStr)) {
//
//                weakSelf.page_num = 1;
//                [weakSelf loadData];
//            }else {
//                [weakSelf showStateText:@"请输入要查询的内容!"];
//            }
            weakSelf.page_num = 1;
            [weakSelf loadData];
            
        };
    }
    return _searchHeaderView;
}

- (MM_HomeSearchDataView *)searchDataView {
    
    if (_searchDataView == nil) {
        
        _searchDataView = [[MM_HomeSearchDataView alloc] initWithFrame:CGRectMake(0, self.searchHeaderView.MM_Max_Y, ScreenW, ScreenH - NaviBarHeight - self.searchHeaderView.MM_h - 44 - HomeIndicatorHeight)];
        _searchDataView.hidden = YES;
        WS(weakSelf);
        _searchDataView.scroBlock = ^{
            [weakSelf.view endEditing:YES];
        };
        
        _searchDataView.searchBlock = ^(MM_HomeSearchRightModel * _Nonnull model) {
          
            [weakSelf.view endEditing:YES];
            weakSelf.searchDataView.hidden = YES;
            weakSelf.searchHeaderView.searchStr = model.location_name;
            weakSelf.locationNumberSearchStr = model.location_name;
            weakSelf.page_num = 1;
            [weakSelf loadData];
        };
        
    }
    return _searchDataView;
}

- (UITableView *)tableView {
    
    if (_tableView == nil) {
        
        _tableView = [UITableView estimatedLayoutTableview:CGRectMake(0, 40, ScreenW, self.scrollView.MM_h - 40) tableviewStyle:UITableViewStyleGrouped delegateAndDataSource:self estimatedSectionHeader:YES estimatedSectionFooter:YES];
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

- (NSMutableArray *)dataArr {
    
    if (_dataArr == nil) {
        
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (void)setupRefresh {
    
    WS(weakSelf);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //请求数据
        weakSelf.page_num = 1;
        [weakSelf loadData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.page_num += 1;
        [weakSelf loadData];
    }];
}

- (void)endRefersh {
    if (self.tableView.mj_header) {
        [self.tableView.mj_header endRefreshing];
    }
    if (self.tableView.mj_footer) {
        [self.tableView.mj_footer endRefreshing];
    }
}

@end
